//
//  RecommandQRCodeView.m
//  PPDL
//
//  Created by 王洋 on 2019/9/4.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "RecommandQRCodeView.h"

@implementation RecommandQRCodeView

- (void)awakeFromNib {
    [super awakeFromNib];

    self.recommandLab.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPre:)];
    [self.recommandLab addGestureRecognizer:longPress];

}

// 使label能够成为响应事件，为了能接收到事件（能成为第一响应者）
- (BOOL)canBecomeFirstResponder{
    return YES;
}
// 可以控制响应的方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:));
}

- (void)copy:(id)sender{
    
    //UIPasteboard：该类支持写入和读取数据，类似剪贴板
    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];    
    NSString *copyStr = [self.recommandLab.text stringByReplacingOccurrencesOfString:@"推荐码：" withString:@""];
    pasteBoard.string = copyStr;
}

// 处理长按事件
- (void)longPre:(UILongPressGestureRecognizer *)recognizer{
    [self becomeFirstResponder]; // 用于UIMenuController显示，缺一不可
    
    //UIMenuController：可以通过这个类实现点击内容，或者长按内容时展示出复制等选择的项，每个选项都是一个UIMenuItem对象
    UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(longPreCopy)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    [[UIMenuController sharedMenuController] setTargetRect:self.recommandLab.frame inView:self.recommandLab.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.iconImageView pp_clipCorners:UIRectCornerAllCorners radius:40 border:1 color:PP_Default_Color_Red_FF3737];

    [self.QRBGView pp_clipCorners:UIRectCornerAllCorners radius:10];
    self.QRBGView.clipsToBounds = NO;
}

- (void)fillBGWithModel:(RecommandModel *)model {

    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.itemUserIconUrl] placeholderImage:[UIImage imageNamed:@"placeholder_rect"] options:SDWebImageRetryFailed];
//    [self.QRCodeImage sd_setImageWithURL:[NSURL URLWithString:model.itemQRCodeImageUrl] placeholderImage:[UIImage imageNamed:@"placeholder_rect"] options:SDWebImageRetryFailed];
    self.userName.text = model.itemUserName;
    self.recommandLab.text = [NSString stringWithFormat:@"推荐码：%@",model.itemRecommandCode];
    

    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *string = model.share_HtmlUrl; //加入二维码url
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];

    CIImage *image = [filter outputImage];
    CGAffineTransform transform = CGAffineTransformMakeScale(10.0f, 10.0f);
    CIImage *output = [image imageByApplyingTransform: transform];
    UIImage *newImage = [UIImage imageWithCIImage:output scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    
    self.QRCodeImage.image = newImage;
}

- (IBAction)shareClick:(id)sender {
    
    if (self.shareActionBlock) {
        self.shareActionBlock();
    }
}

@end
